home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue72 / misc / nielsen / modules / Get_Info.pm < prev    next >
Encoding:
Perl POD Document  |  2002-08-14  |  4.9 KB  |  176 lines

  1. #!/usr/bin/perl
  2.  
  3. #              Create Functions for Perl/PostgreSQL version 1.0
  4.  
  5. #                       Copyright 2001, Mark Nielsen
  6. #                            All rights reserved.
  7. #    This Copyright notice was copied and modified from the Perl 
  8. #    Copyright notice. 
  9. #    This program is free software; you can redistribute it and/or modify
  10. #    it under the terms of either:
  11.  
  12. #        a) the GNU General Public License as published by the Free
  13. #        Software Foundation; either version 1, or (at your option) any
  14. #        later version, or
  15.  
  16. #        b) the "Artistic License" which comes with this Kit.
  17.  
  18. #    This program is distributed in the hope that it will be useful,
  19. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either
  21. #    the GNU General Public License or the Artistic License for more details.
  22.  
  23. #    You should have received a copy of the Artistic License with this
  24. #    Kit, in the file named "Artistic".  If not, I'll be glad to provide one.
  25.  
  26. #    You should also have received a copy of the GNU General Public License
  27. #   along with this program in the file named "Copying". If not, write to the 
  28. #   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
  29. #    02111-1307, USA or visit their web page on the internet at
  30. #    http://www.gnu.org/copyleft/gpl.html.
  31.  
  32. package SAMPLE::Get_Info;
  33.  
  34. use strict;
  35. use Apache;
  36. use DBI;
  37. use CGI;
  38. use SAMPLE::Misc;
  39. use SAMPLE::Constants;
  40.  
  41. sub new 
  42. {
  43. my $Class = shift;
  44. my $self = {};
  45.  
  46. my $Global = SAMPLE::Constants::Get_Constants;
  47.   ### Get the database connection.
  48. $self->{'dbh'} = $Global->{'dbh'};
  49.  
  50. if (exists $ENV{'REMOTE_USER'}) 
  51.   {
  52.   my $Username = $ENV{'REMOTE_USER'};
  53.  
  54.   my $dbh = $self->{'dbh'};
  55.   my $Command = "select * from users where username = ?";
  56.   my $sth = $dbh->prepare($Command);
  57.   $sth->execute($Username);
  58.   $self->{'users'} =  $sth->fetchrow_hashref();
  59.   }
  60. else {$self->{'users'} = {};}
  61.  
  62. bless $self,$Class;
  63.  
  64. return ($self);
  65. }
  66. #--------------------------------------------------------------------------
  67.  
  68. sub Info
  69. {
  70. my $self = shift;
  71. my $Table = shift;
  72. my $Id = shift;
  73.  
  74. my $Info = {};
  75.  
  76.   ### We should have a way to error out here instead of returning nothing.
  77. if (!($Table =~ /[a-z]/i)) {return ($Info);}
  78. if ($Id < 1) {return($Info);}
  79.  
  80. my @Valid_Tables = ( 'class', 'contact', 'students', 'users');
  81.  
  82. if (!(grep($_ eq $Table, @Valid_Tables))) {return ($Info);}
  83.  
  84. my $dbh = $self->{'dbh'};
  85. my $Command = "select * from $Table where $Table\_id = ?";
  86. my $sth = $dbh->prepare($Command);
  87. $sth->execute($Id);
  88. my $ref = $sth->fetchrow_hashref();
  89.  
  90. return ($ref);
  91. }
  92.  
  93. #----------------------------------------------------------------------
  94. # This method is used to create a temporary table which we can use.
  95. # For persistent database connections, this can be a problem. 
  96. # We should create a method to destroy this table when it is done
  97. # being used. 
  98. sub Get_Temp_Table_Name
  99. {
  100. my $self = shift;
  101.  
  102. my $dbh = $self->{'dbh'};
  103. my $Sql = "select sql_temp_table_insert() as name";
  104. my $sth = $dbh->prepare($Sql);
  105. $sth->execute();
  106. my $ref = $sth->fetchrow_hashref;
  107. my $Name = $ref->{'name'};
  108.  
  109. return ("temp_table_$Name");
  110. }
  111.  
  112. #--------------------------------------------------------------------
  113. sub List
  114. {
  115. my $self = shift;
  116. my $Table = shift;
  117. my $Type = shift;
  118. my $Fields = shift;
  119.  
  120. my $Info = {};
  121.  
  122.   ### Return nothing if table is not specified. We should error out instead.
  123. if (!($Table =~ /[a-z]/i)) {return ($Info);}
  124. my @Valid_Tables = ( 'class', 'contact', 'students', 'users');
  125. if (!(grep($_ eq $Table, @Valid_Tables))) {return ($Info);}
  126.  
  127.   ### This determines if we want active, inactive, or all rows. 
  128. my $Clause = "active = 1";
  129. if ($Type eq "inactive") {$Clause = "active = 0";}
  130. elsif ($Type eq "all") {$Clause = "active = 1 or active = 0";}
  131.  
  132.   ### This determines if we want all information,or just the row id and status.
  133. my $HowMuch = "$Table\_id, active";
  134. if ($Fields eq "all") {$HowMuch = '*';}
  135.  
  136. my $dbh = $self->{'dbh'};
  137. my $Command = "select $HowMuch 
  138.    from $Table where $Clause";
  139. my $sth = $dbh->prepare($Command);
  140. $sth->execute();
  141.   ### We put it all into an associative array by table_id primary key. 
  142.   ### IF your data is huge, you will clog up your memory. 
  143.   ### Remember, we are selecting a lot or all of the elements in the table. 
  144. while (my $ref = $sth->fetchrow_hashref()) 
  145.   {
  146.   my $Key = $ref->{"$Table\_id"};
  147.   $Info->{$Key} = $ref;
  148.   }
  149.  
  150.   ### Sometime in the future, we should be able to select which fields
  151.   ### we want returned, and not just the two choices above. 
  152.   ### We will need to make this method use keys as arguments and not
  153.   ### by using @_.
  154.  
  155.   ## We should add the option to restrict by users_id later. 
  156.  
  157. return ($Info);
  158. }
  159.  
  160. #---------------------------------------------------------------------
  161. sub Get_Items
  162. {
  163. my $self = shift;
  164. my $Table = shift;
  165.  
  166. ### This was going to be used to get rows of various tables. 
  167. ### Ignore it now. 
  168.  
  169. return (1);
  170. }
  171.  
  172.  
  173.  
  174.  
  175. 1;
  176.